/*
 * Author: nicolas.bredeche(@)upmc.fr
 * Created on 2005-08-31 -- Refactored on 2014-01-06 
 */

package picoevo.ext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import picoevo.core.*;
import picoevo.tools.*;

public abstract class SelectionOperator_rankBased extends Operator {

	public SelectionOperator_rankBased() { super("MuLambda selection operator"); }
	public SelectionOperator_rankBased(String __name) { super(__name); }

	/** create a view with only the __nb best individuals - the view is ordered 
	 * Remarks:
	 * 1. method is static
 	 * 2. return full population ordered by rank if __nb==popsize
	*/
	static public PopulationView apply ( PopulationObject __population, int __nb )
	{
		// TODO : testing purpose. should be rewritten.
		ArrayList<Individual> list = new ArrayList<Individual>();
		List list2 = (List)((ArrayList)__population.getPopulation()).clone();
		
		Collections.sort(list2,Collections.reverseOrder());
		
		for ( int i = 0 ; i != __nb && i != list2.size() ; i++ )
		{
			list.add((Individual)list2.get(i));
			//Display.debug("("+i+") selected individual with fitness "+((Individual)list.get(i)).getFitness());
		}
		return (new PopulationView(list)); 
	}
}
